<?
require_once(dirname(__FILE__) . '/../../classes/module_interface.inc.php');
require_once(dirname(__FILE__) . '/config.inc.php');

class LoggerModuleDay implements LoggerModuleInterface
{
    protected $database;

    public function __construct(LoggerDatabase $database) 
    {
        $this->database = $database;
    }

    public function PrepareParseSession()
    {
        return true;
    }

    public function ParseHit(&$hit)
    {
        if (isset($hit['modules']['robots']))
            return false;

        $time       = '\'' . $hit['source']['time'] . '\'';
        $visitor    = intval($hit['source']['visitor']);
        $session    = intval($hit['source']['session']);

        $this->database->Query('LOCK TABLES `[PREF]module_dayrw` WRITE, `[PREF]module_dayrw_archive` WRITE');

        $newVisitor = 0;
        $newSession = 0;
        $SelectSQL = 'SELECT COUNT(*) AS `cnt` FROM `[PREF]module_dayrw` WHERE `date`=DATE('.$time.') AND `visitor`='.$visitor.' AND `session`='.$session;
        $result = $this->database->Query($SelectSQL);
        if (!$result)
            trigger_error('Database Error: ' . $this->database->Error(), E_USER_ERROR);
        $row = $this->database->FetchAssoc($result);
        if($row['cnt'] == 0)
        {
            $newSession = 1;

            $SelectSQL = 'SELECT COUNT(*) AS `cnt` FROM `[PREF]module_dayrw` WHERE `date`=DATE('.$time.') AND `visitor`='.$visitor;
            $result = $this->database->Query($SelectSQL);
            if (!$result)
                trigger_error('Database Error: ' . $this->database->Error(), E_USER_ERROR);
            $row = $this->database->FetchAssoc($result);
            if($row['cnt'] == 0)
                $newVisitor = 1;
        }

        $UpdateSQL = 'UPDATE `[PREF]module_dayrw_archive` SET `count`=`count`+1, `visitors`=`visitors`+'.intval($newVisitor).', `sessions`=`sessions`+'.intval($newSession).' ';
        $UpdateSQL .= 'WHERE `date`=DATE('.$time.') AND `hour`=HOUR('.$time.')';
        $result = $this->database->Query($UpdateSQL);
        if (!$result)
            trigger_error('Database Error: ' . $this->database->Error(), E_USER_ERROR);

        if ($this->database->AffectedRows() == 0)
        {
            $InsertSQL = 'INSERT INTO `[PREF]module_dayrw_archive` (`date`, `hour`, `count`, `visitors`, `sessions`) ';
            $InsertSQL .= 'VALUES(DATE('.$time.'), HOUR('.$time.'), 1, 1, 1)';
            $result = @$this->database->Query($InsertSQL);
        }

        if ($newSession)
        {
            $InsertSQL = 'INSERT INTO `[PREF]module_dayrw` (`date`,`visitor`,`session`) VALUES(DATE('.$time.'),'.$visitor.','.$session.')';
            $result = @$this->database->Query($InsertSQL);
        }

        $this->database->Query('UNLOCK TABLES');
    
        $hit['modules']['day']['newVisitor'] = false;

        return true;
    }

    public function Archive()
    {
        $date = LoggerModuleDayConfig::GetArchiveDate();
        $dateEscaped = '\''.mysql_escape_string($date).'\'';

        $this->database->Query('LOCK TABLES `[PREF]module_dayrw` WRITE');
        $DeleteSQL = 'DELETE FROM `[PREF]module_dayrw` WHERE `date`<'.$dateEscaped;
        $this->database->Query($DeleteSQL);
        $this->database->Query('UNLOCK TABLES');

        $OptimizeSQL = 'OPTIMIZE TABLE `[PREF]module_dayrw`';
        $this->database->Query($OptimizeSQL);
        $OptimizeSQL = 'OPTIMIZE TABLE `[PREF]module_dayrw_archive`';
        $this->database->Query($OptimizeSQL);
    }

    public function Purge()
    {
        $date = LoggerModuleDayConfig::GetPurgeDate();
        if ($date != '1970-01-01')
        {
            $dateEscaped = '\''.mysql_escape_string($date).'\'';
    
            $DeleteSQL = 'DELETE FROM `[PREF]module_dayrw` WHERE `time`<'.$dateEscaped;
            $this->database->Query($DeleteSQL);
            $DeleteSQL = 'DELETE FROM `[PREF]module_dayrw_archive` WHERE `date`<'.$dateEscaped;
            $this->database->Query($DeleteSQL);
        }

        $OptimizeSQL = 'OPTIMIZE TABLE `[PREF]module_dayrw`';
        $this->database->Query($OptimizeSQL);
        $OptimizeSQL = 'OPTIMIZE TABLE `[PREF]module_dayrw_archive`';
        $this->database->Query($OptimizeSQL);

        return true;
    }
}